//
//  LWPictureCollectionView.swift
//  Microblog
//
//  Created by Roman on 17/1/10.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit

private let pictureCellId = "LWPictureCellID"

class LWPictureCollectionView: UICollectionView {
    
    var pic_urls: [LWPicturModel]? {
        didSet {
            //数据一旦设置完毕之后就会执行到这里
            //刷新数据
            self.reloadData()
        }
    }

    //从xib中加载collectionView
    //使用自定义cell --> 先注册自定义cell
    override func awakeFromNib() {
        //不需要super
        //注册自定义cell
        register(LWPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        //设置数据源
        self.dataSource = self
    }
    
}

extension LWPictureCollectionView:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureCellId, for: indexPath) as! LWPictureCell
        //显示图片数据
        cell.LWPicturModel = self.pic_urls![indexPath.item]
        
        return cell
        
    }
}
